package com.application.config;

import com.application.util.LoginInterceptor;
import com.application.util.RefreshTokenInterceptor;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;

/**
 * 拦截处理
 * @since 2023-1-16
 * @author  hyj
 * @version  1.0
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {


    @Autowired
    private ObjectMapper objectMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                // 这里就是 不需要拦截的路径
                .excludePathPatterns(
                        "/users/login",
                        "/test/**",
                        "/activityMember/**"
                ).order(1);
        // 我们还需要一个  拦截器  进行 任何时刻的拦截   用来更新  用户在redis中的生成时间
        // 这里需要传入  stringRedisTemplate实例  因为  拦截器中 没有添加任何 标签  只是一个普通的类  并没有归springboot管理
        // 所以 我们需要将  在这里  引入的  stringRedisTemplate  传到  拦截器当中
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                // 这里就是 添加 拦截所有路径  到 刷新登录
                .addPathPatterns("/**").order(0);
    }

    /**
     *防止@EnableMvc把静态资源覆盖了
     * @param registry
     */
     public  void addResourceHandlers(ResourceHandlerRegistry registry){
        //解决静态资源
         registry.addResourceHandler("/**")
                 .addResourceLocations("classpath:/static/");
         //swagger
         registry.addResourceHandler("/swagger-ui.html")
                 .addResourceLocations("classpath:/META-INF/resources/");
         //swagger无法访问js
         registry.addResourceHandler("/webjars/**")
                 .addResourceLocations("classpath:/META-INF/resources/webjars/");
     }


    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
        MappingJackson2HttpMessageConverter converter =new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(objectMapper);
        return converter;
    }
    @Bean
    ObjectMapper objectMapper(){
        ObjectMapper om =new ObjectMapper();
        om.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
        om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss"));
        return  om;
    }

}
